package com.findmeastore.action;

import java.util.List;

import org.springframework.beans.BeanUtils;

import com.findmeastore.bc.ILocalityBc;
import com.findmeastore.bo.CityBo;
import com.findmeastore.bo.LocalityBo;
import com.findmeastore.criteria.LocalityCriteria;
import com.findmeastore.dto.LocalityDto;
import com.findmeastore.pagination.Page;
import com.findmeastore.pagination.PageCriteria;

public class LocalityCrudAction {
	private Integer id;
	private LocalityDto localityDto;
	private List<LocalityBo> localities;
	private List<CityBo> cities;
	private ILocalityBc localityBc;
	private String action;
	
	//search form elements
	private Integer pgNo;
	private Page page;
	private Integer cityId;
	private String nameLike;
	
	public String execute() {
		if ("save".equals(action)) {
			return save();
		} else if ("list".equals(action)) {
			return list();
		} else if ("edit".equals(action)) {
			return edit();
		}
		return list();
	}
	
	public String list() {
		PageCriteria pageCriteria = new PageCriteria();
		if(pgNo!=null&&pgNo>0){
			pageCriteria.setPageNumber(pgNo);
		}else {
			pgNo=1;
		}
		pageCriteria.setPageSize(10);
		LocalityCriteria localityCriteria = new LocalityCriteria();
		if(cityId!=null&&cityId>0){
			localityCriteria.setCityId(cityId);
		}
		if(nameLike!=null&&!nameLike.trim().equalsIgnoreCase("")){
			localityCriteria.setNameLike(nameLike.replace("*", "%"));
		}
		Page page = localityBc.getLocalitiesPageByCriteria(localityCriteria, pageCriteria);
		setLocalities((List<LocalityBo>)page.getContents());
		setPage(page);
		setCities(localityBc.getAllCities());
		return "LIST";
	}

	public String edit() {
		if (id != null && id > 0) {
			LocalityBo localityBo = localityBc.getLocalityById(id);
			if (localityBo != null) {
				localityDto = new LocalityDto();
				BeanUtils.copyProperties(localityBo, localityDto);
			}
		}
		LocalityCriteria criteria = new LocalityCriteria();
		if(localityDto!=null){
			criteria.setCityId(localityDto.getCityId());
		}
		setLocalities(localityBc.getLocalitiesByCriteria(criteria));
		setCities(localityBc.getAllCities());
		return "EDIT";
	}

	public String save() {
		if (localityDto.getId() == null) {
			localityDto.setCreatedById(1);
		}
		localityDto.setModifiedById(1);
		if(localityDto.getParentLocalityId()==-1){
			localityDto.setParentLocalityId(null);
		}
		localityBc.saveLocality(localityDto);
		return "REDIRECT";
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public LocalityDto getLocalityDto() {
		return localityDto;
	}

	public void setLocalityDto(LocalityDto localityDto) {
		this.localityDto = localityDto;
	}

	public List<LocalityBo> getLocalities() {
		return localities;
	}

	public void setLocalities(List<LocalityBo> localities) {
		this.localities = localities;
	}

	public ILocalityBc getLocalityBc() {
		return localityBc;
	}

	public void setLocalityBc(ILocalityBc localityBc) {
		this.localityBc = localityBc;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public List<CityBo> getCities() {
		return cities;
	}

	public void setCities(List<CityBo> cities) {
		this.cities = cities;
	}

	public Integer getPgNo() {
		return pgNo;
	}

	public void setPgNo(Integer pgNo) {
		this.pgNo = pgNo;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public Integer getCityId() {
		return cityId;
	}

	public void setCityId(Integer cityId) {
		this.cityId = cityId;
	}

	public String getNameLike() {
		return nameLike;
	}

	public void setNameLike(String nameLike) {
		this.nameLike = nameLike;
	}
}
